#pragma once

#include <iostream>
#include <unistd.h>
#include <cstring>
#include <string>

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <fcntl.h>
#include "log.hpp"


enum
{
    SocketError = 2,
    BindError,
    ListenError,
    NON_BLOCK_ERR,

};

const int backlog = 10;

class Sock
{
public:
    Sock()
    {
    }

    ~Sock()
    {
    }

    void Socket()
    {
        sockfd_ = socket(AF_INET, SOCK_STREAM, 0);
        if (sockfd_ < 0)
        {
            lg(Fatal, "socker error, %s:%d", strerror(errno), errno);
            exit(SocketError);
        }
        int opt = 1;
        setsockopt(sockfd_, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt));
    }

    void Bind(uint16_t port)
    {
        // 填充好 本地套接字 信息
        struct sockaddr_in local;
        memset(&local, 0, sizeof(local));
        local.sin_family = AF_INET;
        local.sin_port = htons(port);
        local.sin_addr.s_addr = INADDR_ANY;
        
        // 绑定
        socklen_t len = sizeof(local);
        int n = bind(sockfd_, (struct sockaddr *)&local, len);
        if(n < 0)
        {
            lg(Fatal, "bind error, %s:%d", strerror(errno), errno);
            exit(BindError);
        }

    }

    void Listen()
    {
        int n = listen(sockfd_, backlog);
        if(n < 0)
        {
            lg(Fatal, "listen error, %s:%d", strerror(errno), errno);
            exit(ListenError);
        }
    }

    int Accept(std::string *clientip, uint16_t *clientport)
    {
        struct sockaddr_in peer;
        socklen_t len = sizeof(peer);
        int newfd = accept(sockfd_, (struct sockaddr *)&peer, &len);
        if(newfd < 0)
        {
            lg(Warning, "accept error, %s:%d", strerror(errno), errno);
            return -1;
        }
        
        char ipstr[64];
        inet_ntop(AF_INET, &(peer.sin_addr), ipstr, sizeof(ipstr));
        *clientip = ipstr;

        *clientport = peer.sin_port;
        
        return newfd;
    }

    bool Connect(const std::string &ip, const uint16_t &port)
    {
        struct sockaddr_in peer;
        memset(&peer, 0, sizeof(peer));
        peer.sin_family = AF_INET;
        peer.sin_port = htons(port);
        inet_pton(AF_INET, ip.c_str(), &(peer.sin_addr));

        int n = connect(sockfd_, (struct sockaddr *)&peer, sizeof(peer));
        if(n == -1)
        {
            std::cerr << "connect to " << ip << ":" << port << "error" << std::endl;
            return false;
        }

        return true;
    }

    void Close()
    {
        close(sockfd_);
    }

    int Fd()
    {
        return sockfd_;
    }

    // void SetNonBlockOrDie()   // 将文件描述符 设置为非阻塞
    // {
    //     int fl = fcntl(sockfd_, F_GETFL);
    //     if(fl < 0)
    //     {
    //         exit(NON_BLOCK_ERR);
    //     }
    //     fcntl(sockfd_, F_SETFL, fl | O_NONBLOCK);
    // }

private:
    int sockfd_;
};